import { contextBridge, ipcRenderer } from 'electron';

// 暴露安全的 API 给渲染进程
contextBridge.exposeInMainWorld('electronAPI', {
  // 截图相关 API
  captureScreen: () => ipcRenderer.invoke('capture-screen'),
  saveScreenshot: (dataURL: string, filename?: string) => 
    ipcRenderer.invoke('save-screenshot', dataURL, filename),
  copyToClipboard: (dataURL: string) => ipcRenderer.invoke('copy-to-clipboard', dataURL),

  closeScreenshotWindow: () => ipcRenderer.invoke('close-screenshot-window'),
  minimizeMainWindow: () => ipcRenderer.invoke('minimize-main-window'),
  getSources: () => ipcRenderer.invoke('get-sources'),
  getCurrentScreenshot: () => ipcRenderer.invoke('get-current-screenshot'),
  selectSaveFolder: () => ipcRenderer.invoke('select-save-folder'),
  getSaveFolder: () => ipcRenderer.invoke('get-save-folder'),
  resetSaveFolder: () => ipcRenderer.invoke('reset-save-folder'),
  setShortcut: (shortcut: string) => ipcRenderer.invoke('set-shortcut', shortcut),
  getShortcut: () => ipcRenderer.invoke('get-shortcut'),
  resetShortcut: () => ipcRenderer.invoke('reset-shortcut'),
  validateShortcut: (shortcut: string) => ipcRenderer.invoke('validate-shortcut', shortcut),
  notifyScreenshotSaved: (screenshotInfo: { filename: string; dataURL: string; savedPath: string }) => 
    ipcRenderer.invoke('notify-screenshot-saved', screenshotInfo),
  
  // 平台信息
  platform: process.platform,
  
  // 事件监听
  on: (channel: string, callback: (...args: any[]) => void) => {
    ipcRenderer.on(channel, callback);
  },
  
  removeAllListeners: (channel: string) => {
    ipcRenderer.removeAllListeners(channel);
  }
});

// 类型定义
declare global {
  interface Window {
    electronAPI: {
      captureScreen: () => Promise<string[]>;
      saveScreenshot: (dataURL: string, filename?: string) => Promise<string>;
      copyToClipboard: (dataURL: string) => Promise<boolean>;

      closeScreenshotWindow: () => Promise<void>;
      minimizeMainWindow: () => Promise<void>;
      getSources: () => Promise<any[]>;
      getCurrentScreenshot: () => Promise<string | null>;
      selectSaveFolder: () => Promise<string | null>;
      getSaveFolder: () => Promise<string>;
      resetSaveFolder: () => Promise<string>;
      setShortcut: (shortcut: string) => Promise<{ success: boolean; message: string }>;
      getShortcut: () => Promise<string>;
      resetShortcut: () => Promise<{ success: boolean; shortcut: string; message: string }>;
      validateShortcut: (shortcut: string) => Promise<{ valid: boolean; message: string }>;
      notifyScreenshotSaved: (screenshotInfo: { filename: string; dataURL: string; savedPath: string }) => Promise<boolean>;
      platform: string;
      on: (channel: string, callback: (...args: any[]) => void) => void;
      removeAllListeners: (channel: string) => void;
    };
  }
}